package org.example.java8.stream;

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class NumericStream {
    public static void main(String[] args) {
        Stream<Integer> stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        Stream<Integer> integerStream = stream.filter(i -> i.intValue() > 3);
        Integer result = integerStream.reduce(0, Integer::sum);
        System.out.println(result);

        stream = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
        //Integer内存占用量要比int大很多，int是4byte/32bit，于是提供了IntStream可以方便的使用拆箱后的结果
        //IntStream intStream=stream.mapToInt(i->i.intValue());
        IntStream intStream = stream.mapToInt(Integer::intValue);
        //intStream.filter(i -> i > 3).reduce(0, (i, j) -> i + j);
        intStream.filter(i -> i > 3).sum();

        pythagoreanTriples();
    }


    //勾股定理
    private static void pythagoreanTriples() {
        int a = 9;
        //IntStream intStream = IntStream.rangeClosed(1, 100)
        //        .filter(b -> Math.sqrt(a * a + b * b) % 1 == 0);
        //intStream.forEach(System.out::println);

        IntStream.rangeClosed(1, 100)
                .filter(b -> Math.sqrt(a * a + b * b) % 1 == 0)
                //boxed数值流在进行collect操作之前，要使用boxed进行装箱操作
                //三种常见的数值流（貌似只有这三种）IntStream、DoubleStream、LongStream
                .boxed().map(b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)})
                .forEach(r -> System.out.println("a=" + r[0] + " b=" + r[1] + " c=" + r[2]));

        //和上边的方式效果一样
        IntStream.rangeClosed(1, 100)
                .filter(b -> Math.sqrt(a * a + b * b) % 1 == 0)
                .mapToObj(b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)})
                .forEach(r -> System.out.println("a=" + r[0] + " b=" + r[1] + " c=" + r[2]));

    }

}
